import django_filters
from django.shortcuts import render
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
from .models import Room,Reservation,Hostel
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .serial import RoomSerializer
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from .tool import method
# Create your views here.


class Page(PageNumberPagination):
    page_size = 2
    page_size_query_param = 'size'
    page_query_param = 'page'


class RoomList(ListAPIView):
    serializer_class = RoomSerializer
    pagination_class = Page

    def get_queryset(self):
        data = Room.objects.filter(display=True).order_by('id')
        return data



class RoomDetail(ListAPIView):
    serializer_class = RoomSerializer

    def get_queryset(self):
        id = self.request.GET.get('id')
        data = Room.objects.filter(id=id).order_by('id')
        return data


class ReservationView(APIView):

    def post(self,request):
        data = request.data
        room = Room.objects.get(id=data['room'])
        name = data['name']
        phone = data['phone']
        price = room.price
        message = data['message']
        time = data['time']
        reservation = Reservation.objects.create(room=room,name=name,phone=phone,price=price,message=message,time=time)
        if reservation:
            room.display = False
            room.save()
            return Response({"status": "success"}, status=status.HTTP_201_CREATED)
        else:
            return Response({"status": "error"}, status=status.HTTP_400_BAD_REQUEST)



class RoomSearch(ListAPIView):
    pagination_class = Page
    serializer_class = RoomSerializer


    def get_queryset(self):
        name = self.request.GET.get('name')
        data = Room.objects.filter(name__contains=name).order_by('id')
        data = data.filter(display=True).order_by('id')
        return data



class Query(APIView):

    def get(self,request):
        data = self.request.GET.get('ask')
        res = method(data)
        return Response({"message":res})






















